import React from 'react';
import {
  AsyncStorage,
} from 'react-native';

import BaseModelNew from './BaseModelNew';
import ResponseConstants from '../common/ResponseConstants';
import Config from '../config';

export default class CommonModel extends BaseModelNew {
  constructor() {
    super();
  }

  setup(dataManager, callback) {
    this.dataManager = dataManager;

    this.requestSildersImages = this.requestSildersImages.bind(this);
    this.requestADSildersImages = this.requestADSildersImages.bind(this);
    this.requestUrlForContent = this.requestUrlForContent.bind(this);
    this.requestLive = this.requestLive.bind(this);
    this.getSaveVersion = this.getSaveVersion.bind(this);
    this.saveVersion = this.saveVersion.bind(this);
    this.slideClick = this.slideClick.bind(this);
    this.sharePoint = this.sharePoint.bind(this);
    this._urlDatas = [];
    this._urlDataIsCall = [];
    this._saveDatas = {version:{version_code:0}};

    this._callback = callback;
    this._dealCount = 0;

    this._readSaveData();
    // this._initHealthyKitData();
  }

  clearCache() {
    this._urlDatas = [];
  }

  _tryCallback() {
    // console.log("_tryCallback", this._dealCount);
    this._dealCount = this._dealCount - 1;
    if(this._dealCount === 0) {
      this._callback(this._saveDatas.version);
    }
  }

  _readSaveData() {
    this._dealCount = this._dealCount + 1;
    AsyncStorage.getItem("version",function(errs,result){
          //TODO:错误处理
          if (!errs) {
            if(result == null) {
              this._saveDatas.version = {version_code:0};
            }
            else {
              let versionData = JSON.parse(result);
              this._saveDatas.version = versionData;
            }
            // console.log('Get versionData success', result);
          }
          else {
            this._saveDatas.version = {version_code:0};
            // console.log('Get versionData error');
          }
          this._tryCallback();
      }.bind(this));
  }

  //版本数据
  getSaveVersion() {
    return this._saveDatas.version;
  }

  saveVersion(version_code) {
    this._saveDatas.version.version_code = version_code;
    AsyncStorage.setItem("version", JSON.stringify(this._saveDatas.version) ,function(errs){
          //TODO:错误处理
          if (errs) {
            console.log('存储versionData错误');
          }
          if (!errs) {
            console.log('存储versionData无误');
          }
      });
  }

  // 图片滚动器
  //http://118.89.111.124:8080/dqapp/slides?page=1&page_size=10&tag=0
  requestSildersImages(tag, callback) {
    var url = Config.serverUrl + "/slides?page=1&page_size=10&tag=" + tag;
    // if (tag===2) {
    //   url = Config.serverUrl + "/admin/slides?page=1&page_size=10&sort_id=1&totalItems=50&totalPage=5&tag=" + tag;
    // }
    console.log('------'+url)
    this.sendGet(url,
      callback);
  }
  //公告
  //http://127.0.0.1:40000/dqapp/boards/9/posts?home_page=0&page=1&page_size=10&sort_id=2&state=1&tag=2&totalItems=30&totalPage=3
  requestADSildersImages(data,callback){
    var url = Config.serverUrl + "/boards/9/posts?home_page=0&sort_id=2&state=1&totalItems=10&totalPage=1&tag=" + data.tag;
    if (data.page_size) {
      url+= '&page_size='+data.page_size;
    }else{
      url+= '&page_size=10'
    }
    if (data.page) {
      url+= '&page='+data.page
    }else{
      url+= '&page=1'
    }
    console.log(url)
    this.sendGet(url,
      callback);
  }
  //http://43.241.76.189:40000/dqapp/admin/live?home_page=1&page=1&page_size=10&sort_id=0&state=1&totalItems=1&totalPage=1
  requestLive(tag,callback){
    var url = Config.serverUrl + "/admin/live?home_page=1&page=1&page_size=10&sort_id=0&state=1&totalItems=1&totalPage=1";
    this.sendGet(url,
      callback);
  }
    // 图片滚动器
  //http://118.89.111.124:8080/dqapp/slides?page=1&page_size=10&tag=0
  slideClick(tag) {
    var url = Config.serverUrl + "/slides/" + tag.slide_id+"/click";
    this.sendPost(url,JSON.stringify({}), function(){});
  }

  sharePoint(data){
     var url = Config.serverUrl + "/shares";
     var postData = {
      type:data.type,
      target_id:data.id
     }
     this.sendPost(url,JSON.stringify(postData), function(){});
  }

  //请求url数据
  requestUrlForContent(data, callback) {
    let url = data.url;
    if(this._urlDatas[url]) {

      callback(this._urlDatas[url], true);
    }
    else {
      // 多次重复请求了，去掉
      if(this._urlDataIsCall[url]){
        return;
      }
      this._urlDataIsCall[url] = true;
      this.sendGet(url,
        (data, isSucceed, response) => {this._requestUrlForContentCallback(data, isSucceed, response, url, callback)},
        ResponseConstants.responseType_text
      )
    }
  }

  _requestUrlForContentCallback(data, isSucceed, response, url, callback) {
    if(isSucceed) {
      this._urlDatas[url] = data;
    }
    this._urlDataIsCall[url] = false;
    callback(data, isSucceed, response);
  }
}
